﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmSumpSectionAnnotation : System.Windows.Forms.Form
	{
		public List<SumpParamSet> SumpParamSets
		{
			get
			{
				return this.sumpParamSets;
			}
		}

		public FrmSumpSectionAnnotation()
		{
			DataReadWrite.InitializeFilePath(this.xmlPath);
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		private void FrmSumpSectionAnnotation_Load(object sender, EventArgs e)
		{
			this.LoadParamData();
			this.DvgNotSortable(this.dgvParamSet);
			this.DvgSetColumWidth(this.dgvParamSet);
			this.dgvParamSet.ClearSelection();
		}

		private void btnInserParam_Click(object sender, EventArgs e)
		{
			List<int> list = new List<int>();
			foreach (object obj in this.dgvParamSet.SelectedCells)
			{
				int index = ((DataGridViewCell)obj).OwningRow.Index;
				if (!list.Contains(index))
				{
					list.Add(index);
				}
			}
			if (list.Count > 0)
			{
				list.Sort();
				this.selectIndex = list.ElementAt(list.Count - 1);
				DataRow row = this.dtData.NewRow();
				this.dtData.Rows.InsertAt(row, this.selectIndex + 1);
				this.dgvParamSet.DataSource = this.dtData;
			}
		}

		private void btnDeletParam_Click(object sender, EventArgs e)
		{
			try
			{
				List<int> list = new List<int>();
				foreach (object obj in this.dgvParamSet.SelectedCells)
				{
					int index = ((DataGridViewCell)obj).OwningRow.Index;
					if (!list.Contains(index))
					{
						list.Add(index);
					}
				}
				if (list.Count > 0)
				{
					foreach (int index2 in list)
					{
						this.dgvParamSet.Rows.RemoveAt(index2);
					}
				}
				this.dgvParamSet.ClearSelection();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Warning(ex.Message);
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.BindingDara())
			{
				this.DataSave();
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Show("参数值存在漏项，请检查！");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void dgvParamSet_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			this.cellValue = this.dgvParamSet.CurrentCell.Value.ToString();
		}

		private void dgvParamSet_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 0 && this.dgvParamSet.CurrentCell.Value.ToString().Trim() == "")
			{
				this.dgvParamSet.CurrentCell.Value = this.cellValue;
				YJKMessageBox.Show("标高名称不能为空！");
			}
			if (e.ColumnIndex == 1 && !this.regex.IsMatch(this.dgvParamSet.CurrentCell.Value.ToString().Trim()))
			{
				this.dgvParamSet.CurrentCell.Value = this.cellValue;
				YJKMessageBox.Show("您输入的“相对高度”格式不正确，应为正整数或零，且不能为空！");
			}
		}

		private void LoadParamData()
		{
			this.dtData.Columns.Add("标高名称");
			this.dtData.Columns.Add("相对高度(mm)");
			string text = DataReadWrite.ReadValue(this.xmlPath, "SumpParamNode", "ParamName");
			string text2 = DataReadWrite.ReadValue(this.xmlPath, "SumpParamNode", "RelativeHeight");
			if (text == "" || text2 == "")
			{
				this.drData = this.dtData.NewRow();
				this.drData[0] = "停泵液位";
				this.drData[1] = "300";
				this.dtData.Rows.Add(this.drData);
				this.drData = this.dtData.NewRow();
				this.drData[0] = "开泵液位";
				this.drData[1] = "800";
				this.dtData.Rows.Add(this.drData);
				this.drData = this.dtData.NewRow();
				this.drData[0] = "报警液位";
				this.drData[1] = "1500";
				this.dtData.Rows.Add(this.drData);
				this.drData = this.dtData.NewRow();
				this.drData[0] = "地面标高";
				this.drData[1] = "2000";
				this.dtData.Rows.Add(this.drData);
			}
			else
			{
				char[] separator = new char[]
				{
					','
				};
				string[] array = text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
				string[] array2 = text2.Split(separator, StringSplitOptions.RemoveEmptyEntries);
				int num = 0;
				foreach (string text3 in array)
				{
					this.drData = this.dtData.NewRow();
					this.drData[0] = array[num];
					this.drData[1] = array2[num];
					this.dtData.Rows.Add(this.drData);
					num++;
				}
			}
			this.dgvParamSet.DataSource = this.dtData;
		}

		private void DvgNotSortable(DataGridView dgv)
		{
			for (int i = 0; i < dgv.Columns.Count; i++)
			{
				dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
			}
		}

		private void DvgSetColumWidth(DataGridView dgv)
		{
			dgv.Columns[0].Width = 32;
			dgv.Columns[0].Width = 88;
		}

		private bool BindingDara()
		{
			this.sumpParamSets.Clear();
			List<SumpParamSet> list = new List<SumpParamSet>();
			bool result = true;
			for (int i = 0; i < this.dgvParamSet.RowCount; i++)
			{
				if (this.dgvParamSet.Rows[i].Cells[0].Value.ToString().Trim() == "" || this.dgvParamSet.Rows[i].Cells[1].Value.ToString().Trim() == "")
				{
					result = false;
					break;
				}
				this.sumpParamSet = new SumpParamSet();
				this.sumpParamSet.ParamName = this.dgvParamSet.Rows[i].Cells[0].Value.ToString();
				this.sumpParamSet.RelativeHeight = double.Parse(this.dgvParamSet.Rows[i].Cells[1].Value.ToString()) / 304.8;
				list.Add(this.sumpParamSet);
			}
			this.sumpParamSets = (from sPS in list
			orderby sPS.RelativeHeight
			select sPS).ToList<SumpParamSet>();
			return result;
		}

		private void DataSave()
		{
			string text = "";
			string text2 = "";
			foreach (SumpParamSet sumpParamSet in this.sumpParamSets)
			{
				text = text + sumpParamSet.ParamName + ",";
				text2 = text2 + (sumpParamSet.RelativeHeight * 304.8).ToString() + ",";
			}
			DataReadWrite.WriteValue(this.xmlPath, "SumpParamNode", "ParamName", text);
			DataReadWrite.WriteValue(this.xmlPath, "SumpParamNode", "RelativeHeight", text2);
		}

		private DataTable dtData = new DataTable();

		private DataRow drData;

		private SumpParamSet sumpParamSet;

		private List<SumpParamSet> sumpParamSets = new List<SumpParamSet>();

		private string xmlPath = Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml");

		private int selectIndex;

		private string cellValue;

		private Regex regex = new Regex("^[0-9]\\d*$");
	}
}
